package com.ext.aic.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import com.ext.aic.AICApplication;

public class AppUtils {
    private static volatile AppUtils instance;

    public static AppUtils getInstance() {
        if (instance == null) {
            synchronized (AppUtils.class) {
                if (instance == null) {
                    instance = new AppUtils();
                }
            }
        }
        return instance;
    }

    public long getVersionCode() {
        try {
            PackageInfo packageInfo = null;
            String packageName = AICApplication.getInstance().getPackageName();
            PackageManager packageManager = AICApplication.getInstance().getPackageManager();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                packageInfo = packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0));
            } else {
                // 旧版本兼容
                packageInfo = packageManager.getPackageInfo(packageName, 0);
            }
            return packageInfo.getLongVersionCode();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getVersionName() {
        try {
            PackageInfo packageInfo = null;
            String packageName = AICApplication.getInstance().getPackageName();
            PackageManager packageManager = AICApplication.getInstance().getPackageManager();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                packageInfo = packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0));
                return packageInfo.versionName;
            } else {
                packageInfo = packageManager.getPackageInfo(packageName, 0);
                return packageInfo.versionName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
